package com.pet.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.backend.pojo.Contact;
import com.pet.backend.pojo.Vo;
import com.pet.backend.service.impl.ContactServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  联系我们
 * </p>
 *
 * @author pet
 * @since 2024-07-13
 */
@CrossOrigin
@RestController
@RequestMapping("/contact")
public class ContactController {

    @Resource
    private ContactServiceImpl contactService;

    // 按照名字搜索
    @GetMapping("/search/{name}/{current}")
    public Vo<Contact> search(@PathVariable("name") String name, @PathVariable("current") Integer current){
        if (name.equals("")){
            return new Vo<>();
        }
        LambdaQueryWrapper<Contact> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Contact::getName,name);
        Page<Contact> contactPage = new Page<>(current <= 0 ? 1 : current,6);
        IPage<Contact> page = contactService.page(contactPage, wrapper);
        if (page.getTotal() == 0){
            // 表示没有数据
            return new Vo<>();
        }
        List<Contact> contacts = page.getRecords();
        // 总条数
        long total = page.getTotal();
        // 当前页
        long pageCurrent = page.getCurrent();
        // 返回
        return new Vo<>(true,contacts,pageCurrent,total);
    }

    // 分页查询
    @GetMapping("/page/{current}")
    public Vo<Contact> page(@PathVariable("current") Integer current) {
        Page<Contact> contactPage = new Page<>(current >= 0 ? current : 1, 6);
        IPage<Contact> page = contactService.page(contactPage);
        if (page.getTotal() == 0) {
            // 表示没有数据直接返回
            return new Vo<>(false, "目前没有联系我们的！");
        }
        // 数据
        List<Contact> contacts = page.getRecords();
        // 当前页
        long curr = page.getCurrent();
        // 总记录数
        long total = page.getTotal();
        // 返回
        return new Vo<>(true, contacts, curr, total);
    }

    // 修改状态
    @GetMapping("/updateStu/{id}")
    public Vo<Contact> updateStu(@PathVariable("id") String id){
        Contact contact = new Contact();
        contact.setId(id);
        contact.setStatus("已读");
        boolean b = contactService.updateById(contact);
        if (b) {
            return new Vo<>(b,"确定已读！");
        }else {
            return new Vo<>(b, "修改失败，请稍后重试！");
        }
    }

    // 删除
    @GetMapping("/delete/{id}")
    public Vo<Contact> delete(@PathVariable("id") String id){
        boolean b = contactService.removeById(id);
        if (b){
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }

    // 新增
    @PostMapping("/insert")
    public Vo<Contact> insert(@RequestBody Contact contact){
        contact.setStatus("未读");
        boolean b = contactService.save(contact);
        if (b){
            return new Vo<>(b,"信息发送成功！");
        }else {
            return new Vo<>(b,"信息发送失败，请稍后重试！");
        }
    }
}
